<script>
    $(function () {
        {% if not mail %}
        SelfRedirect('您还未配置邮件，无法使用远程支持配置功能，请先进行邮件配置？')
        {% endif %}
    });
    function submit_support() {
        var user_contact = $("#support_body").find("#user_contact");
        var main_contact = $("#support_body").find("#main_contact");
        var main_address = $("#support_body").find("#main_address");
        var main_callhome = $("#support_body").find("#main_callhome");
        var contact_info = $("#support_body").find("#contact_info");
        var bValid = true;

        var objs = [
            { 'el': user_contact, 'regs': [
                {'regexp': /^[\u4e00-\u9fa5a-zA-Z0-9()]+$/i, 'text': '应由汉字、字母、数字和括号组成。'}
            ], 'text': '单位名称不能为空。'},
            { 'el': main_contact, 'regs': [
                {'regexp': /^[\u4e00-\u9fa5a-zA-Z]+$/i, 'text': '应由汉字和字母组成。'}
            ], 'text': '联系人不能为空。'},
            { 'el': contact_info,'regs':[
                {'regexp': [/^1[3|4|5|7|8][0-9]{9}$/i, /^0\d{2,3}-?\d{7,8}$/], 'text': '电话号码格式不正确。'}
            ], 'text': '电话不能为空。'},
            { 'el': main_address,'regs': [
                {'regexp': /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/i, 'text': '邮件地址格式不正确。'}
            ], 'text': '邮件地址不能为空。'}
        ];

        var result = checkEnableSubmit(objs);
        var jqtip = $('.error-tip');
        jqtip.css({
            'top': '-9999px',
            'left': '-9999px'
        });
        $("#support_body").find('input').css({
            'border': '1px solid #d8dce5'
        });
        if (result.enableSubmit === false) {
            var $el = result.el;
            var $parent = $el.parent();
            var top = $parent[0].offsetTop;
            var left = $parent[0].offsetLeft;
            var jqtip = $('.error-tip');
            jqtip.find('p').text(result.text);
            jqtip.css({
                'top': (top - 35) + 'px',
                'left': left + 'px'
            });
            $el.css({
                'border': '1px solid #fa575f'
            });
            return;
        }

        var post_data = {
            "company": $.trim(user_contact.val()),
            "contact": $.trim(main_contact.val()),
            "address": $.trim(main_address.val()),
            "telephone": $.trim(contact_info.val()),
            "callhome": main_callhome.is(':checked'),
        }
        $('.rtn-tip').slideUp('fast');
        $.post("/customer/support/create", post_data, function (data) {
            if (data.reply.is_success) {
                var customer_mail_id = data.records.id
                $("#support_body").find("#customer_id").val(customer_mail_id)
                $("#support_delete").attr('disabled', false);
                $('.rtn-tip').text('保存配置完成').slideDown('fast');
            } else {
                var error = data.reply.error;
                $('.rtn-tip').text(error).slideDown('fast');
            }
        });
    }

    function support_delete() {
        $("#support_body input[type=password], input[type=text]").each(function () {
            $(this).val('');
        })
        $("#support_delete").attr('disabled', true);
    }

    function submit_support_delete() {
        var customer_mail_id = $("#customer_id").attr("value");
        var post_data = {'id': customer_mail_id};
        $('.rtn-tip').slideUp('fast');
        $.post("/customer/support/delete", post_data,
            function (data) {
                if (data.reply.is_success) {
                    $('.rtn-tip').text('删除配置完成').slideDown('fast');
                    support_delete();
                } else {
                    var error = data.reply.error;
                    $('.rtn-tip').text(error).slideDown('fast');
                }
            });
    }
</script>
<style>
    .title {
        padding: 20px 30px;
    }

    .title h3 {
        margin: 0;
    }

    .config-box {
        position: relative;
        margin: 0 30px;
        padding: 30px 40px;
        border-bottom: 1px solid #eee;
        background: #fff;
    }

    .config-box .config-item {
        margin-top: 15px;
    }

    .config-box .config-item input {
        width: 180px;
    }

    .config-box .config-item label {
        margin: 0;
    }

    .config-box span {
        color: #f00;
    }

    .footer {
        margin: 0 30px;
        padding: 20px 40px 30px 40px;
        background: #fff;
    }

    .rtn-tip {
        width: 50%;
        background-color: #ED7441;
        font-size: 14px;
        color: #fff;
        border-radius: 3px !important;
        display: none;
        padding: 3px 10px;
    }
    .config-item-question{
        color:#999999;
    }
    .config-item-question input{
        margin-right: 8px;
    }
</style>
<div class="title">
    <h3>远程支持</h3>
</div>
<div class="config-box" id="support_body">
    <div class="rtn-tip"></div>
    <input style="display:none" type="text" id="customer_id" value="{{customer.id}}"/>
    <div class="config-item">
        <div class="item-key">
            <label>单位名称</label><span>*</span>
        </div>
        <div class="item-input">
            <input type="text" id="user_contact" maxlength="16" value="{{customer.company}}"/>
        </div>
    </div>

    <div class="config-item">
        <div class="item-key">
            <label>联系人</label><span>*</span>
        </div>
        <div class="item-input">
            <input type="text" id="main_contact" maxlength="16" value="{{customer.contact}}"/>
        </div>
    </div>

    <div class="config-item">
        <div class="item-key">
            <label>联系电话</label><span>*</span>
        </div>
        <div class="item-input">
            <input id="contact_info" type="text" maxlength="16" value="{{customer.telephone}}"/>
        </div>
    </div>

    <div class="config-item">
        <div class="item-key">
            <label>邮件地址</label><span>*</span>
        </div>
        <div class="item-input">
            <input type="text" id="main_address" value="{{customer.address}}">
        </div>
    </div>
    <div class="config-item-question">
        <div class="item-key">
            <label>Callhome</label><span></span>
        </div>
        <div class="item-input">
            {% if customer.callhome %}
            <input type="checkbox" id="main_callhome" checked=true />
            {% else %}
            <input type="checkbox" id="main_callhome" />
            {% endif %}
            启用Callhome功能，存储系统运行状态及日志相关信息将通过邮件每天发送到我方维护平台，维护平台通过自动分析及预测，提前预防系统潜在问题的发生。
        </div>
    </div>
    <div class="error-tip">
        <p></p>
        <span></span></div>
</div>
<div class="footer" ng-controller="MailModalCtrl">
    <button class="btn btn-blue" ng-click="submit_customer()">[[ 'label save'|translate ]]</button>
    {% if customer %}
    <button class="btn btn-blue" id="support_delete" ng-click="support_delete()" style="margin-left:10px">删除远程支持</button>
    {% else %}
    <button class="btn btn-blue" id="support_delete" ng-click="support_delete()" style="margin-left:10px" disabled>
        删除远程支持
    </button>
    {% endif %}
</div>

